package JDBC;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        try (ServerSocket server = new ServerSocket(8080)) {
            Socket socket = server.accept();
            System.out.println("接收到来自客户端的连接：" + socket.getRemoteSocketAddress() + "：" + socket.getPort());
            //使用while不断读取来自客户端的数据
            InputStream stream = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = stream.read(buffer)) > 0) {   //当客户端没有数据发送时，read会处于阻塞状态
                System.out.println("接受到客户端数据: " + new String(buffer, 0, len));
                out.write(("已收到长度为 " + len + " 字节的数据").getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

